package cn.edu.hqu.javaee.springboot_h2;

import cn.edu.hqu.javaee.springboot_h2.entity.User;
import cn.edu.hqu.javaee.springboot_h2.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.math.BigDecimal;


@SpringBootApplication
public class SpringBootH2Application {
    private static final Logger log = LoggerFactory.getLogger(SpringBootH2Application.class);
    public static void main(String[] args) {

        SpringApplication.run(SpringBootH2Application.class, args);
    }
    @Bean
    public CommandLineRunner demo(UserRepository repository) {
        return (args) -> {
            BigDecimal bg1 = new BigDecimal("1");
            BigDecimal bg2 = new BigDecimal("10");
            BigDecimal bg3 = new BigDecimal("100");
            BigDecimal bg4 = new BigDecimal("1000");
            BigDecimal bg5 = new BigDecimal("10000");
            // save a few Users
            repository.save(new User("UJack", "Jack", (short) 18,bg1));
            repository.save(new User("UChloe", "Chloe", (short) 19,bg2));
            repository.save(new User("UKim", "Kim", (short) 20,bg3));
            repository.save(new User("UDavid", "David", (short) 21,bg4));
            repository.save(new User("UMichelle", "Michelle", (short) 22,bg5));

            // fetch all users
            log.info("Users found with findAll():");
            log.info("-------------------------------");
            for (User user : repository.findAll()) {
                log.info(user.toString());
            }
            log.info("");

            // fetch an individual user by ID
            User user = repository.findById(1L);
            log.info("User found with findById(1L):");
            log.info("--------------------------------");
            log.info(user.toString());
            log.info("");

            // fetch users by username
            log.info("User found with findByUsername('UKim'):");
            log.info("--------------------------------------------");
            repository.findByUsername("UKim").forEach(bauer -> {
                log.info(bauer.toString());
            });
            log.info("");
        };
    }

}
